package com.pushtalk;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.pushtalk.dao.BussDAO;
import com.pushtalk.service.ListenBussTask;
import com.pushtalk.service.PushTask;
import com.pushtalk.utils.IMUtil;
import com.pushtalk.utils.LogUtil;

/**
 * 启动类
 * @author wx
 *
 */
public class MainPush {

	private static Logger logger  =  Logger.getLogger(MainPush.class);
	private static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);

	public static void main(String[] args) {
		 PropertyConfigurator.configure("log4j.properties");	 
		 //配置表初始化
		 Config.initConfig();
		 //xmpp推送系统帐户登录
		 StartXMPPClient();
		 //任务初始化，并启动拉取数据的任务
		 StartPullService();
		 //启动推送任务		 
		 if (Config.StopPushService.equals("no"))
		 StartPushService();
	}
	
	private static void StartXMPPClient() {
		IMUtil.loginXMPP(Config.PushAccountUserName, Config.PushAccountPassword);
	}

	public static void StartPullService(){
		
		if (Config.ClearDataByInit.equals("yes"))
			ClearData();		
		
		ListenBussTask pullTask = new ListenBussTask();		
		scheduledThreadPool.scheduleAtFixedRate(pullTask, 1, Config.ListenInterval, TimeUnit.SECONDS);
		
		LogUtil.debug(logger, "Pull data monitoring service status(拉取服务)：ok");
	}
	
	private static void StartPushService() {
		//该任务用于将tkBussNotify数据库数据，由特定账号如OA、BPM推送给IM用户			
		PushTask pushTask = new PushTask();
		scheduledThreadPool.scheduleAtFixedRate(pushTask, 30, Config.PushInterval, TimeUnit.SECONDS);

		LogUtil.debug(logger, "Push service status(推送服务)：ok");
	}

	private static void ClearData() {
		LogUtil.debug(logger, "Clean up unpushed data：start");
		BussDAO.DeleteAllNotify();
		LogUtil.debug(logger, "Clean up unpushed data：ok");
	}

}
